package com.kl.modular.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.kl.common.exception.SystemException;
import com.kl.common.pager.PageParameter;
import com.kl.common.pager.Pager;
import com.kl.modular.system.dao.bo.SystemAuthResourceBo;
import com.kl.modular.system.dao.mapper.SystemAuthResourceMapper;
import com.kl.modular.system.dao.model.SystemAuthResource;
import com.kl.modular.system.service.api.ISystemAuthResourceService;
import com.kl.modular.system.service.constant.SystemConStant;
import com.kl.modular.system.service.vo.SystemAuthResourceVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 权限资源serviceImpl业务实现
 * @author XRP_ZJB
 * @date 2016-01-15 13:41
 * @(C) XIGNSOFT Copyright 
 */
@Service
public class SystemAuthResourceServiceImpl implements ISystemAuthResourceService {
	
	private static final Logger logger = LoggerFactory.getLogger(SystemAuthResourceServiceImpl.class);
	
	@Autowired
	private SystemAuthResourceMapper systemAuthResourceMapper;
	
	public Pager<SystemAuthResource> getListByBo(SystemAuthResourceBo systemAuthResourceBo, PageParameter pageParameter) {
		pageParameter.setPageBoolean(true);
		List<SystemAuthResource> systemAdminList = systemAuthResourceMapper.getListByBoPage(systemAuthResourceBo, pageParameter);
		return new Pager<SystemAuthResource>(pageParameter, systemAdminList);
	}

	public void addSystemAuthResource(SystemAuthResource systemAuthResource) throws Exception {
		int result = systemAuthResourceMapper.insert(systemAuthResource);
		if(result == 0){
			logger.error("添加权限资源发生错误>>data:"+JSON.toJSONString(systemAuthResource));
			throw new SystemException("添加权限资源发生错误");
		}
	}

	public void updateByCode(SystemAuthResource systemAuthResource) throws Exception {
		int result = systemAuthResourceMapper.updateByPrimaryKey(systemAuthResource);
		if(result ==0){
			logger.error("更新权限资源发生错误>>data:"+JSON.toJSONString(systemAuthResource));
			throw new SystemException("更新权限资源发生错误");
		}
	}

	public void deleteByCodes(String[] codes) throws Exception {
		SystemAuthResourceBo bo = new SystemAuthResourceBo();
		bo.setAuthCodes(codes);
		int parentCounts = selectCountByBo(bo);
		if(parentCounts > 0){
			logger.error("删除权限资源发生错误>>该权限资源下级有数据，暂不能删除");
			throw new SystemException("该权限资源下级含有数据，暂不能删除");
		}
		int result = systemAuthResourceMapper.deleteByPrimaryKey(codes);
		if(result ==0){
			logger.error("删除权限资源发生错误>>data:"+JSON.toJSONString(codes));
			throw new SystemException("删除权限资源发生错误");
		}
	}

	public SystemAuthResource getSystemAuthResourceByCode(String code){
		SystemAuthResource systemAuthResource = systemAuthResourceMapper.selectByPrimaryKey(code);
		return systemAuthResource;
	}
	
	/**
	 * 模块下权限信息/不包括功能权限
	 */
	public SystemAuthResourceVO getSystemAuthResourceVOByModularCode(String code , String roleShiroCode , String roleCodes) throws Exception {
		SystemAuthResourceVO systemAuthResourceVO = new SystemAuthResourceVO();
		
		// 查询模块下一级菜单
		List<SystemAuthResource> oneSystemAdminList = getSystemAuthResourceOneByModularCode(code ,roleShiroCode,roleCodes);
		
		// 查询二级菜单
		List<SystemAuthResource> twoSystemAdminList = getSystemAuthResourceTwoByModularCode(code,roleShiroCode,roleCodes);
		
		// 模块权限信息 
		LinkedHashMap<SystemAuthResource, List<SystemAuthResource>> systemAuthResourceMap = new LinkedHashMap<SystemAuthResource, List<SystemAuthResource>>();
		for (int i = 0; i < oneSystemAdminList.size(); i++) {
			List<SystemAuthResource> systemAuthResourceList = new ArrayList<SystemAuthResource>();
			SystemAuthResource oneSystemAuthResource = oneSystemAdminList.get(i);
			for (int j = 0; j < twoSystemAdminList.size(); j++) {
				SystemAuthResource twoSystemAuthResource = twoSystemAdminList.get(j);
				if (StringUtils.equals(oneSystemAuthResource.getAuthCode(), twoSystemAuthResource.getAuthparentCode())) {
					systemAuthResourceList.add(twoSystemAuthResource);
				}
			}
			systemAuthResourceMap.put(oneSystemAuthResource, systemAuthResourceList);
		}
		
		systemAuthResourceVO.setOneSystemAuthResourceList(oneSystemAdminList);
		systemAuthResourceVO.setMenuMapList(systemAuthResourceMap);
		
		return systemAuthResourceVO;
	}

	/**
	 * 公安菜单权限查询
	 * @param code
	 * @param personId
	 * @return
	 * @throws Exception
	 */
	public SystemAuthResourceVO getSystemAuthResourceVoForSec(String code, Long personId) throws Exception {
		SystemAuthResourceVO systemAuthResourceVO = new SystemAuthResourceVO();

		// 查询模块下一级菜单
        List<SystemAuthResource> menuAuthList = systemAuthResourceMapper.getMenuAuthListByOrgansPersonId(code, personId, null);
        // 查询二级菜单
        String[] systemAuthResourceCodes = new String[menuAuthList.size()];
        int i = 0;
        for (SystemAuthResource systemAuthResource : menuAuthList) {
            systemAuthResourceCodes[i] = systemAuthResource.getAuthCode();
            i++;
        }
        if(systemAuthResourceCodes.length == 0){
            return null;
        }

        List<SystemAuthResource> twoMenuAuthList = systemAuthResourceMapper
                .getMenuAuthListByOrgansPersonId(null, personId, systemAuthResourceCodes);

        // 模块权限信息
        LinkedHashMap<SystemAuthResource, List<SystemAuthResource>> systemAuthResourceMap = new LinkedHashMap<SystemAuthResource, List<SystemAuthResource>>();
        for (int h = 0; h < menuAuthList.size(); h++) {
            List<SystemAuthResource> systemAuthResourceList = new ArrayList<SystemAuthResource>();
            SystemAuthResource oneSystemAuthResource = menuAuthList.get(h);
            for (int j = 0; j < twoMenuAuthList.size(); j++) {
                SystemAuthResource twoSystemAuthResource = twoMenuAuthList.get(j);
                if (StringUtils.equals(oneSystemAuthResource.getAuthCode(), twoSystemAuthResource.getAuthparentCode())) {
                    systemAuthResourceList.add(twoSystemAuthResource);
                }
            }
            systemAuthResourceMap.put(oneSystemAuthResource, systemAuthResourceList);
        }

        systemAuthResourceVO.setOneSystemAuthResourceList(menuAuthList);
        systemAuthResourceVO.setMenuMapList(systemAuthResourceMap);


        return systemAuthResourceVO;
	}

	/**
	 * 模块下权限信息/一级菜单
	 */
	public List<SystemAuthResource> getSystemAuthResourceOneByModularCode(String modularCode, String roleShiroCode, String roleCodes)
			throws Exception {
		SystemAuthResourceBo systemAuthResourceBo = new SystemAuthResourceBo();
		systemAuthResourceBo.setAuthParentCode(modularCode);
		systemAuthResourceBo.setAuthType(SystemConStant.SYSTEM_AUTH_TYPE_MENU);
		systemAuthResourceBo.setRoleShiroCode(roleShiroCode);
		systemAuthResourceBo.setRoleCodes(roleCodes.split(","));
		PageParameter pageParameter = new PageParameter();
		pageParameter.setPageBoolean(PageParameter.PAGE_BOOLEAN_FALSE);
		List<SystemAuthResource> systemAdminList = systemAuthResourceMapper.getListByBoPage(systemAuthResourceBo, pageParameter);
		return systemAdminList;
	}
	
	/**
	 * 模块下权限信息/二级菜单
	 */
	public List<SystemAuthResource> getSystemAuthResourceTwoByModularCode(String menuCodes, String roleShiroCode , String roleCodes) throws Exception {
		List<SystemAuthResource> systemAdminList = getSystemAuthResourceOneByModularCode(menuCodes, roleShiroCode, roleCodes);
		String[] systemAuthResourceCodes = new String[systemAdminList.size()];
		int i = 0;
		for (SystemAuthResource systemAuthResource : systemAdminList) {
			systemAuthResourceCodes[i] = systemAuthResource.getAuthCode();
			i++;
		}
		SystemAuthResourceBo twoSystemAuthResourceBo = new SystemAuthResourceBo();
		twoSystemAuthResourceBo.setAuthType(SystemConStant.SYSTEM_AUTH_TYPE_MENU);
		twoSystemAuthResourceBo.setAuthCodes(systemAuthResourceCodes);
		twoSystemAuthResourceBo.setRoleShiroCode(roleShiroCode);
		twoSystemAuthResourceBo.setRoleCodes(roleCodes.split(","));
		PageParameter pageParameter = new PageParameter();
		pageParameter.setPageBoolean(PageParameter.PAGE_BOOLEAN_FALSE);
		List<SystemAuthResource> twoSystemAdminList = systemAuthResourceMapper.getListByBoPage(twoSystemAuthResourceBo, pageParameter);
		return twoSystemAdminList;
	}
	
	
	public List<SystemAuthResource> getListAllByBo(SystemAuthResourceBo systemAuthResourceBo , PageParameter pageParameter) {
		return systemAuthResourceMapper.getListByBoPage(systemAuthResourceBo, pageParameter);
	}

	public int selectCountByBo(SystemAuthResourceBo systemAuthResourceBo) {
		return systemAuthResourceMapper.selectCountByBo(systemAuthResourceBo);
	}

	public List<String> getRoleAuthListByRoleCode(String roleCode, String moduleType) {
		return systemAuthResourceMapper.getRoleAuthListByRoleCode(roleCode, moduleType);
	}

	/**
	 * 查询机关人员权限
	 * @param organsPersonId
	 * @return
	 */
	public List<String> getAuthResourceByOrgansPersonId(Long organsPersonId) {
		return systemAuthResourceMapper.getAuthResourceByOrgansPersonId(organsPersonId);
	}

	/**
	 * 查询机关人员权限
	 * @param organsPersonId
	 * @return
	 */
	public List<SystemAuthResource> getMenuListByOrgansPersonId(Long organsPersonId) {
		return systemAuthResourceMapper.getMenuListByOrgansPersonId(organsPersonId);
	}

}
